package ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import main.Def;
import main.ExcelConvert;

public class MainWindow extends MouseAdapter
{
	private static MainWindow s_instance = null;
	
	private JFrame m_window;
	private JPanel m_panel;
	private Table m_table;
	private JScrollPane m_scroll;
	private JScrollPane m_scrollText;
	private JPanel m_upPanel;
	private JPanel m_downPanel;
	private JButton m_add;
	private JButton m_remove;
	private JButton m_run;
	private JButton m_all;
	private JButton m_none;
	private JButton m_clear;
	private JButton m_addConfigSheet;
	private JTextArea m_text;
	
	private MainWindow()
	{
		m_window = new JFrame("ExcelConvert " + Def.VERSION);
		m_panel = new JPanel();
		m_window.setContentPane(m_panel);
		m_panel.setLayout(new BorderLayout());
		m_table = new Table();
		m_scroll = new JScrollPane(m_table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		m_downPanel = new JPanel();
		m_panel.add(m_downPanel, BorderLayout.CENTER);
		m_upPanel = new JPanel();
		m_panel.add(m_upPanel, BorderLayout.NORTH);
		m_upPanel.setLayout(new GridLayout(2, 4));
		m_downPanel.setLayout(new GridLayout(2, 1));
		m_downPanel.add(m_scroll);
		m_add = new JButton("+");
		m_remove = new JButton("-");
		m_run = new JButton("run");
		m_all = new JButton("select all");
		m_none = new JButton("clear select");
		m_clear = new JButton("clear log");
		m_addConfigSheet = new JButton("add config");
		m_upPanel.add(m_clear);
		m_upPanel.add(m_all);
		m_upPanel.add(m_none);
		m_upPanel.add(m_run);
		m_upPanel.add(m_remove);
		m_upPanel.add(m_add);
		m_upPanel.add(m_addConfigSheet);
		m_add.addMouseListener(this);
		m_remove.addMouseListener(this);
		m_run.addMouseListener(this);
		m_all.addMouseListener(this);
		m_none.addMouseListener(this);
		m_clear.addMouseListener(this);
		m_addConfigSheet.addMouseListener(this);
		m_text = new JTextArea();
		m_text.setEditable(false);
		m_text.setLineWrap(true);
		m_scrollText = new JScrollPane(m_text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		m_downPanel.add(m_scrollText);
		System.setOut(new Stream());
		
		m_window.setSize(Def.WIDTH, Def.HEIGHT);
		m_window.setLocation(MouseInfo.getPointerInfo().getLocation().x - Def.WIDTH / 2, MouseInfo.getPointerInfo().getLocation().y);
		m_window.setVisible(true);
		m_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static MainWindow instance()
	{
		if(s_instance == null)
		{
			s_instance = new MainWindow();
		}
		return s_instance;
	}
	
	public JFrame window()
	{
		return m_window;
	}
	
	public void mouseClicked(MouseEvent e)
	{
		if(e.getSource() == m_add)
		{
			m_table.addFile();
		}
		else if(e.getSource() == m_remove)
		{
			m_table.remove();
		}
		else if(e.getSource() == m_run)
		{
			ExcelConvert.clear();
			m_table.run();
		}
		else if(e.getSource() == m_clear)
		{
			m_text.setText("");
		}
		else if(e.getSource() == m_all)
		{
			m_table.selectAll();
		}
		else if(e.getSource() == m_none)
		{
			m_table.clearSelection();
		}
		else if(e.getSource() == m_addConfigSheet)
		{
			String[] paths = m_table.getSelectedWorkBooks();
			ExcelConvert.addConfig(paths);
		}
	}
	
	public void addText(String text)
	{
		m_text.setText(m_text.getText() + text + "\n");
	}
}
